//
//  SUcaiViewController.m
//  WuliHanhan
//
//  Created by chen tang on 16/2/26.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "SUcaiViewController.h"
#import "Header.h"
#import "MJRefresh.h"
#import "MJRefreshStateHeader.h"

#import "SucaiCollectionViewCell.h"

#import "SearchStarViewController.h"
#import "DownLoadViewController.h"



#import "StarGuanZhuScrollView.h"



#import "WuLiRequestTypeStarAPI.h"
#import "WuLiRequestFlowStar.h"
#import "UserInfoModel.h"

@interface SUcaiViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,StarGuanZhuScrollViewDelegate>
{
    UIView                      *guanzhuTitleView;
    UICollectionView            *mainCollectionView;
    
    NSMutableArray              *dataMutableArray;
    
    NSMutableArray              *starListArray;
    
    StarGuanZhuScrollView       *guanzhuScrollView;
    
    SucaiCollectionViewCell     *cell;
    
    NSString                    *pageString;
    NSString                    *_typeIDString;
}

@end

@implementation SUcaiViewController


- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:uicolor(237, 237, 237)];
    
    pageString = @"1";
    starListArray = [NSMutableArray new];
    dataMutableArray = [NSMutableArray new];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WithView, 64)];
    [titleView setBackgroundColor:uicolor(254, 206, 0)];
    [self.view addSubview:titleView];

    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, WithView, 25)];
    [titleLable setTextAlignment:NSTextAlignmentCenter];
    [titleLable setText:@"我的素材"];
    [self.view addSubview:titleLable];

    
    guanzhuTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleView.frame), WithView, 120.0)];
    [self.view addSubview:guanzhuTitleView];
    
    guanzhuScrollView = [[StarGuanZhuScrollView alloc] initWithFrame:CGRectMake(0, 0, WithView, 120.0)];
    [guanzhuTitleView setBackgroundColor:[UIColor whiteColor]];
    [guanzhuTitleView addSubview:guanzhuScrollView];
    
        
    
    UIImage *image = [UIImage imageNamed:@"NoData.png"];
    UIImageView *nodataImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [nodataImageView setCenter:CGPointMake(WithView/2.0, 310.0)];
    [nodataImageView setImage:image];
    [self.view addSubview:nodataImageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nodataImageView.frame)+14.0, WithView, 20.0)];
    [lable setTextAlignment:NSTextAlignmentCenter];
    [lable setTextColor:uicolor(97, 97, 97)];
    [self.view addSubview:lable];
    [lable setText:@"暂无该明星素材"];
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    
    mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 190, WithView, heightView-184-49) collectionViewLayout:flowLayout];
    [mainCollectionView setBackgroundColor:uicolor(237, 237, 237)];
    
    //注册Cell，必须要有
    [mainCollectionView registerClass:[SucaiCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    
    
    [self.view addSubview:mainCollectionView];
    
    
    [WuLiRequestFlowStar requestFlowStarWithUserID:[UserInfoModel userIDString]];
    
    
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(WithView-40, 20, 40, 40)];
    [button setTitle:@"更多" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(pushSearchStarVC) forControlEvents:UIControlEventTouchUpInside];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
                                    
    [mainCollectionView setDataSource:self];
    [mainCollectionView setDelegate:self];
    [guanzhuScrollView setMStarGuanZhuScrollViewDelegate:self];
    [self addJianting];
    
    [guanzhuScrollView.carousel scrollToItemAtIndex:0 animated:YES];
    
    mainCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewMyWorkioData)];
//    mainCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreMyWorkioData)];

}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController setHidesBottomBarWhenPushed:NO];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [self.tabBarController setHidesBottomBarWhenPushed:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//监听
- (void)addJianting
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserFloeStarBack:) name:getFloeStar object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(starTypeGetBack:) name:mingxingbiaoqian object:nil];
}


- (void)searchStasrTypeID:(NSString *)typeIDString
{
    _typeIDString = typeIDString;
    [mainCollectionView.mj_header beginRefreshing];
    
}

//刷新
- (void)loadNewMyWorkioData
{
    [WuLiRequestTypeStarAPI requestStarType:_typeIDString];
}



#pragma mark----获取明星标签
- (void)starTypeGetBack:(NSNotification *)notion
{
    [mainCollectionView.mj_header endRefreshing];
    [mainCollectionView.mj_footer endRefreshing];
    
    
    NSDictionary *dic = [notion userInfo];
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        if ([[dic valueForKey:@"data"] count] == 0)
        {
            [mainCollectionView setHidden:YES];
            return;
        }
        [mainCollectionView setHidden:NO];
        [dataMutableArray removeAllObjects];
        [dataMutableArray addObjectsFromArray:[dic valueForKey:@"data"]];
        [mainCollectionView reloadData];
    }
    else
    {
        [mainCollectionView setHidden:YES];
        return;

    }
}



#pragma mark----获得关注明星
- (void)getUserFloeStarBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
//    NSLog(@"%@", dic);
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        [starListArray removeAllObjects];
        [starListArray addObjectsFromArray:[dic valueForKey:@"data"]];
//        NSLog(@"%@", starListArray);
        [guanzhuTitleView setHidden:NO];
        [mainCollectionView setHidden:NO];
        if (starListArray.count == 0)
        {
            [guanzhuTitleView setHidden:YES];
            [mainCollectionView setHidden:YES];
        }
        else
        {
            [guanzhuScrollView reloadWithArray:starListArray];
        }
        
    }
    else
    {
        
    }
}




-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataMutableArray.count;
}


//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setTag:indexPath.row];
    [cell loadTypeInfo:dataMutableArray[indexPath.row]];
    return cell;
}
#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(biliwith(146.0), biliwith(110.0));
}
//
////定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(4, 10, 4, 10);
}
//
//
//
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 9.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 8.0;
}




#pragma mark---点击进入图片详情
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{    
    DownLoadViewController *downLoad = [[DownLoadViewController alloc]init];
    downLoad.titleNameStr = [NSString stringWithFormat:@"%@",[[[[dataMutableArray objectAtIndex:indexPath.row]valueForKey:@"tags"]objectAtIndex:0 ]valueForKey:@"name" ]];
    downLoad.tagsID = [[[dataMutableArray[indexPath.row] valueForKey:@"tags"] objectAtIndex:0] valueForKey:@"id"];
    downLoad.StarID = [[dataMutableArray[indexPath.row] valueForKey:@"star"] valueForKey:@"id"];
    NSLog(@"%@",[[[[dataMutableArray objectAtIndex:indexPath.row]valueForKey:@"tags"]objectAtIndex:0 ]valueForKey:@"name" ]);
    
    [self.navigationController pushViewController:downLoad animated:YES];
}




- (void)pushSearchStarVC
{
    SearchStarViewController *search = [[SearchStarViewController alloc] init];
    search.starMutableArray = starListArray;
    [self.navigationController pushViewController:search animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
